<?php

/**
 * Erra_Persistance_Session : Class to handle session data
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod, Erra Studios
 * @version $1.0$
 * @access public
 */
class Erra_Persistance_Session {

    public static function getFieldData($item_key) {
        if (is_string($item_key)) {
            if (isset($_SESSION[$item_key])) {
                $data = $_SESSION[$item_key];
                // Its a field ?
                if (isset($data['__fieldPostData'])) {
                    unset($data['__fieldPostData']);
                    return $data;
                }
            }
        }
        return null;
    }

    /**
     * Erra_Persistance_Session::get() : gets a key
     *
     * @param mixed $name - The message id
     * @param mixed $value - The message content
     * @return
     */
    public static function get($item_key) {
        if (is_string($item_key)) {
            if (isset($_SESSION[$item_key])) {
                $data = $_SESSION[$item_key];
                // If its a field, we don't return the data, we rebuild it to send a full featured field
                if (is_array($data) && isset($data['__fieldPostData'])) {
                    $postArray = Erra_System_FormRenderer::getFieldPostData($data['__fieldPostData']);
                    unset($data['__fieldPostData']);
                    $objectId = $postArray['object_id'];
                    $objectClass = $postArray['object_class'];
                    $html_attributes = $postArray['html_attributes'];
                    $description = json_decode($postArray['description']);
                    $help = json_decode($postArray['help']);
                    $id = isset($postArray['id']) ? $postArray['id'] : -1;
                    $buildArgs = !empty($postArray['build_args']) ? Erra_Serialization_SerializerJson::unserializeRaw(html_entity_decode($postArray['build_args'])) : null;
                    // TODO : Factory::buildFieldFromData($fieldData, $rawData=null);
                    if (class_exists($objectClass)) {
                        $field = new $objectClass($buildArgs);
                        $field->setObjectId($objectId);
                        $field->setName($objectId);
                        $field->setAttributes($html_attributes);
                        $field->setDescription($description);
                        $field->setDescription($help);
                        $field->setId($postArray['id']);
                        $field->setRawData($data);
                        return $field;
                    }
                } else {
                    return $_SESSION[$item_key];
                }
            }
            return null;
        }
    }

    public static function bKeyExists($item_key) {

        if (is_string($item_key)) {
            return (isset($_SESSION[$item_key]) && !empty($_SESSION[$item_key])) ? true : false;
        }
    }

    public static function set($item_key, $item) {
        if (is_string($item_key)) {
            // If its a field, we setup all the informations needed for it's reconstruction
            if (is_subclass_of($item, 'Erra_Fields_Base_Object')) {
                $rawData = $item->getRawDataAssoc();
                $fieldData = Erra_System_FormRenderer::buildFieldData($item, null, $item->getBuildArgs());
                $rawData['__fieldPostData'] = $fieldData;
                $_SESSION[$item_key] = $rawData;
            } else {
                $_SESSION[$item_key] = $item;
            }
        }
    }

    public static function remove($item_key) {
        if (self::bKeyExists($item_key)) {
            unset($_SESSION[$item_key]);
        }
    }

}

?>